package ciyuan.Gui;

import ciyuan.Item.ItemRegisterManager;
import ciyuan.Item.TypeInterface.*;
import ciyuan.MainClass;
import net.minecraft.item.*;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.entity.player.ItemTooltipEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.ArrayList;
import java.util.List;

import static ciyuan.Item.ItemGroups.Food;
import static ciyuan.Item.ItemGroups.Machine;

/**
 * @author 150149
 */
@Mod.EventBusSubscriber(value = {Dist.CLIENT}, modid = MainClass.MODID)
public class ShowItemDescription {

    private static final String FoodTimeString = "ciyuanrpgfoodtime";
    private static final String CY_STONEPOT_OUT = "cystonepotout";
    private static final String CY_STONEPOT_COLD = "cystonepotcold";

    private static final String Firstsplit = "ciyuan.gui.dsc1";
    private static final String Lastsplit = "ciyuan.gui.dsc2";

    @SubscribeEvent(priority = EventPriority.LOW)
    public static void onItemTooltipEvent(ItemTooltipEvent event) {
        ItemStack itemStack = event.getItemStack();
        List<ITextComponent> iTextComponentList = new ArrayList<>(event.getToolTip());
        ITextComponent name = iTextComponentList.get(0);
        iTextComponentList.remove(0);
        event.getToolTip().clear();

        appendName(event,itemStack,name);
        event.getToolTip().add(new TranslationTextComponent(Firstsplit));
        appendType(event,itemStack);

        if (canAppendFood(event, itemStack) || canAppendStonePot(event, itemStack) || !iTextComponentList.isEmpty() || canAppendSkillBook(itemStack)) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc0"));
            if (canAppendFood(event, itemStack)) {
                appendFood(event, itemStack);
            }
            if (canAppendStonePot(event, itemStack)) {
                appendSTONEPOT(event, itemStack);
            }
            if (canAppendSkillBook(itemStack)) {
                appendSkillBook(event, itemStack);
            }
            if (!iTextComponentList.isEmpty()) {
                event.getToolTip().addAll(iTextComponentList);
            }
        }
        event.getToolTip().add(new TranslationTextComponent(Lastsplit));
    }

    private static void appendType(ItemTooltipEvent event,ItemStack itemStack) {
        if (itemStack.getItem().getFoodProperties() != null || itemStack.getItem() instanceof FoodType) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc3"));
        } else if ((itemStack.getItem().getCreativeTabs().contains(Food) && itemStack.getItem() instanceof BlockItem) || (itemStack.getItem().getCreativeTabs().contains(ItemGroup.TAB_MATERIALS) && itemStack.getItem() instanceof BlockItem)) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc13"));
        } else if (itemStack.getItem().getCreativeTabs().contains(Machine)) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc17"));
        } else if (itemStack.getItem().getCreativeTabs().contains(ItemGroup.TAB_DECORATIONS)) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc5"));
        } else if (itemStack.getItem() instanceof BlockItem) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc6"));
        } else if (itemStack.getItem() instanceof DyeItem) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc7"));
        } else if (itemStack.getItem() instanceof EnchantedBookItem) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc8"));
        } else if (itemStack.getItem() instanceof PotionItem) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc9"));
        } else if (itemStack.getItem() instanceof ShootableItem) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc10"));
        } else if (itemStack.getItem() instanceof SwordItem ) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc11"));
        } else if (itemStack.getItem() instanceof LiveType) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc12"));
        } else if (itemStack.getItem() instanceof ToolItem || itemStack.getItem().getCreativeTabs().contains(ItemGroup.TAB_TOOLS)) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc14"));
        } else if (itemStack.getItem() instanceof MaterialType || itemStack.getItem().getCreativeTabs().contains(ItemGroup.TAB_MATERIALS)) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc15"));
        } else if (itemStack.getItem() instanceof ComponentType) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc19"));
        }  else {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc16"));
        }

        if (itemStack.getItem() instanceof ALLItemType) {
            event.getToolTip().add(((ALLItemType) itemStack.getItem()).getQuality().getTranslationTag());
        }else {
            for (ItemGroup itemGroup: ItemGroup.TABS) {
                if (itemStack.getItem().getCreativeTabs().contains(itemGroup)) {
                    event.getToolTip().add(new TranslationTextComponent("ciyuan.quality.Normal"));
                    break;
                }
            }
        }
    }

    private static void appendName(ItemTooltipEvent event,ItemStack itemStack,ITextComponent name) {
        event.getToolTip().add(new TranslationTextComponent(name.getString() + " [" + itemStack.getDamageValue() + "/" + itemStack.getMaxDamage() + "]"));
    }

    private static void appendFood(ItemTooltipEvent event,ItemStack itemStack) {
        CompoundNBT compoundNBT = itemStack.getTag();
        if (compoundNBT!=null) {
            if (compoundNBT.contains(FoodTimeString)) {
                long foodTime = compoundNBT.getLong(FoodTimeString);
                long i =100L - (event.getPlayer().getCommandSenderWorld().getGameTime()-foodTime)/1000L;
                event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc4").append(new StringTextComponent(" " + i + "%")));
            }
        }
    }

    private static boolean canAppendFood(ItemTooltipEvent event,ItemStack itemStack) {
        return itemStack.getItem().getFoodProperties()!=null || itemStack.getItem() instanceof FoodType;
    }

    private static void appendSTONEPOT(ItemTooltipEvent event,ItemStack itemStack) {
        CompoundNBT compoundNBT = event.getItemStack().getTag();
        String out = compoundNBT.getString(CY_STONEPOT_OUT);
        if (out.equals(ItemRegisterManager.Aluminium.get().getDescriptionId())) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.tooltip.contains").append(new TranslationTextComponent("item.ciyuanrpg.broken_aluminium")));
        } else if (out.equals(ItemRegisterManager.Copper.get().getDescriptionId())) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.tooltip.contains").append(new TranslationTextComponent("item.ciyuanrpg.broken_copper")));
        } else if (out.equals(Items.GOLD_INGOT.getDescriptionId())) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.tooltip.contains").append(new TranslationTextComponent("item.ciyuanrpg.broken_gold")));
        } else if (out.equals(Items.IRON_INGOT.getDescriptionId())) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.tooltip.contains").append(new TranslationTextComponent("item.ciyuanrpg.broken_iron")));
        } else if (out.equals(ItemRegisterManager.tin.get().getDescriptionId())) {
            event.getToolTip().add(new TranslationTextComponent("ciyuan.tooltip.contains").append(new TranslationTextComponent("item.ciyuanrpg.broken_tin")));
        }

        if (compoundNBT.contains(CY_STONEPOT_COLD)) {
            long foodTime = compoundNBT.getLong(CY_STONEPOT_COLD);
            long i =(event.getPlayer().getCommandSenderWorld().getGameTime()-foodTime)/100L;
            event.getToolTip().add(new TranslationTextComponent("ciyuan.gui.dsc18").append(new StringTextComponent(" " + i + "%")));
        }
    }

    private static boolean canAppendStonePot(ItemTooltipEvent event, ItemStack itemStack) {
        if (event.getItemStack().getItem().getDescriptionId().equals(ItemRegisterManager.stonepot1.get().getDescriptionId())) {
            return event.getItemStack().getTag() != null;
        }
        return false;
    }

    private static void appendSkillBook(ItemTooltipEvent event, ItemStack itemStack) {
        SkillBookType skillBookType = (SkillBookType) itemStack.getItem();
        event.getToolTip().add(skillBookType.getSkillDescription());
    }

    private static boolean canAppendSkillBook(ItemStack itemStack) {
        return itemStack.getItem() instanceof SkillBookType;
    }

}
